package model.mapping;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import view.panels.MapPanel;

import model.ImageStore;
import model.Model;
import model.simulation.Store;

public class MapImageBuilder {

	private Model model;
	private Conversion conversion;
	private Store store;
	
	public MapImageBuilder(Model model) {
		
		this.model = model;
		conversion = new Conversion();
		store = new Store();
	}
	
	public Conversion getConversion() {
		return conversion;
	}

	public void setConversion(Conversion conversion) {
		this.conversion = conversion;
	}
	
	public Store getStore() {
		return store;
	}

	public void setStore(Store store) {
		this.store = store;
	}

	public void clearData() {
		
		store.getIncidences().clear();
	}
	
	public void buildImage() {
		
		MapPanel mapPanel = model.getMapPanel();
		ArrayList<Integer> xs = new ArrayList<Integer>();
		ArrayList<Integer> ys = new ArrayList<Integer>();
		int minx = 0;
		int miny = 0;
		for (int i=0;i<mapPanel.getTilesImages().size();i++) {
			BufferedImage im = conversion.toBufferedImage(mapPanel.getTilesImages().get(i).getImage());
			mapPanel.getTilesImages().get(i).setBImage(im);
			if (mapPanel.getTilesImages().get(i).getdX() < minx)
				minx = mapPanel.getTilesImages().get(i).getdX();
			if (mapPanel.getTilesImages().get(i).getdY() < miny)
				miny = mapPanel.getTilesImages().get(i).getdY();
			
			if (!xs.contains(mapPanel.getTilesImages().get(i).getX())) 
				xs.add(mapPanel.getTilesImages().get(i).getX());
			if (!ys.contains(mapPanel.getTilesImages().get(i).getY()))
				ys.add(mapPanel.getTilesImages().get(i).getY());
		}
		
		int width = mapPanel.getTilesImages().get(0).getBImage().getWidth();
		int height = mapPanel.getTilesImages().get(0).getBImage().getHeight();
		int twidth = width*xs.size();
		int theight = height*ys.size();
		xs.clear();
		ys.clear();
		BufferedImage imagen = new BufferedImage(twidth,theight,BufferedImage.TYPE_INT_RGB);
		Graphics2D g2 = imagen.createGraphics();
		
		Integer contador = 1;
		for (int i=0;i<mapPanel.getTilesImages().size();i++) {
			ImageStore ele = mapPanel.getTilesImages().get(i);
			if (!xs.contains(ele.getX())) 
				xs.add(ele.getX());
			else {
				contador = contador+1;
				xs.clear();
				xs.add(ele.getX());
			}
			g2.drawImage(ele.getImage(),width*xs.size()-width,height*contador-height,mapPanel);
			
		}
		g2.dispose();
	
		minx = Math.abs(minx);
		miny = Math.abs(miny);
		int x0 = width - (width - minx);  
		int x1 = x0 + mapPanel.getWidth();
		int y0 = height - (height - miny);
		int y1 = y0 + mapPanel.getHeight();
		BufferedImage newImage = new BufferedImage(mapPanel.getWidth(),mapPanel.getHeight(),BufferedImage.TYPE_INT_RGB);
		int x = 0;
		int y = 0;
		for (int i=x0;i<x1;i++) {
			for (int j=y0;j<y1;j++) {
				newImage.setRGB(x,y,imagen.getRGB(i,j));
				y = y+1;
			}
			x = x+1;
			y = 0;
		}
		storeImage(newImage);
		model.updateContent(1,store.getMapImage());
	}
	
	public void storeImage(BufferedImage image) {
		
		store.setMapImage(image);
		store.createBlackboardImage(image);
	}
}
